<?php
App::uses('AppController', 'Controller');
/**
 * Users Controller
 *
 * @property User $User
 * @property PaginatorComponent $Paginator
 */
class ForgotsController extends Controller {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator' , 'Auth', 'Session');
    public $name = 'Users';
	function beforeFilter() {
            $this->Auth->allow('active', 'forgot');
	}

/**
 * index method
 *
 * @return void
 */
    public function forgot() {
	    if ($this->request->is('post')) {
            $email = $this->request->data['User']['email'];
            //echo $email;exit;
            $user =  $this->User->find('first', array('conditions' => array('User.email' => $email)));
            if ($user != null && isset($user)){
               // var_dump( $user);
                //exit;
                $actikey = md5($user['User']['email']);
                $this->User->read(null,$user['User']['id']);
                $this->User->set('actikey',$actikey);
                $this->User->save();
                //Debugger::dump($this->User);exit;
                //echo $user['User']['actikey'];exit;
                $message = '
                 <table cellpadding="0" cellspacing="0" border="0" width="100%">
                  <tbody>
                   <tr>
                    <td style="padding:10px;">
                     <p style="font-family:\'helvetica neue\', Arial, Helvetica, sans-serif; color: #666; font-weight:200; font-size: 18px; text-align:center;">
                      Hello '.$user['User']['first_name'].'!
                     </p>
					 <p style="font-family:\'helvetica neue\', Arial, Helvetica, sans-serif; color: #666; font-weight:200; font-size: 18px; text-align:center;">
                      
					  There has been a request to reset your password for Datamyze. To reset your password use this link:
                     </p>
                    
                    </td>
                   </tr>
                   <tr>
                    <!-- Class Data -->
                    <td style="padding:0px 100px;">
                     
                    </td>
                   </tr>
                   <!-- End Class Data -->
                   <tr>
                    <td style="padding: 30px 0px;">
                      <p style="text-align:center; padding:0px; margin:0px;">
                     <a href="http://'.$_SERVER["SERVER_NAME"].'/cakephp/forgots/active?active='.$user['User']['actikey'].'" style="margin-top: 10px;background-color: #79bd42;border: 1px solid #5d9f28;color: #fff;text-decoration: none;font-size: 22px;font-weight: bold;padding: 10px 40px;-moz-border-radius: 5px;-webkit-border-radius: 5px;border-radius: 5px;text-shadow: 2px 2px 2px #5e9533;">
                      Click here to change your password
                     </a>
                    </p>
                    </td>
                   </tr>
				   <tr>
				   <td>
				   <p style="font-family:\'helvetica neue\', Arial, Helvetica, sans-serif; color: #666; font-weight:200; font-size: 18px; text-align:left;">
                      
					  If you did not make a request to reset your password please notify us at support@datamyze.com.
					<br/>
						Best regards,
					<br/>
						Datamyze Team
                     </p>
					 </td>
					 </tr>
                  </tbody>
                 </table>
                 ';
                
     $headers = "From: Datamyze Support<support@datamyze.com>\r\n" .
      "Reply-To: support@datamyze.com \r\n".
      'X-Mailer: PHP/' . phpversion();
     $headers  .= 'MIME-Version: 1.0' . "\r\n";
     $headers .= 'Content-type: text/html; charset=utf8' . "\r\n";
     mail($email, 'Get your password', $message, $headers);
     $this->redirect('/users/login');
            }
            else{
                    //echo 'dd';exit;
                $this->Session->setFlash('Can not find this Email in our system. Please try again.');
            }
            
          //  echo $user['email'];exit;
	    }
	    $this->layout = 'login';
	}

    public function active($actikey = null) {
            //if (isset($this->params['url']['active'])){
                 $actikey=$this->params['url']['active'];
            //echo  $actikey;exit;
             $user = $this->User->find('first', array('conditions' => array('User.actikey' => $actikey)));
             $this->set('actikey',$actikey);
            //Debugger::dump($user);exit;
            if ($this->request->is('post')) {
                    
                if( $this->request->data['User']['password'] == $this->request->data['User']['repassword']){
                    $user = $this->User->find('first', array('conditions' => array('User.email' => $this->request->data['User']['email'])));
                     //$passwordHasher = new SimplePasswordHasher();
                   // $newpass = $passwordHasher->hash($this->request->data['User']['password']);
                    //debug($this->request->data['User']['password']);
                   // debug($newpass);exit;
                    //$newpass= $this->Auth->password($this->request->data['User']['password']);
                    //Debugger::dump($newpass);exit;
                    $this->User->read(null,$user['User']['id']);
                    $this->User->set('password',$this->request->data['User']['password']);
                    if ($this->User->save()){
                       // debug($this->User->read(null,$user['User']['id']));
                        //exit;
                        //$this->Session->setFlash('You can login by new password');
                        $this->redirect('/users/login');
                    }
                    else{
                        $this->Session->setFlash('Can not Save');
                    }
                } else{
                    $this->Session->setFlash('Password and RePassword do not match');
                }
            }
            
            $user = $this->User->read(null,$user['User']['id']);
            $this->set('user',$user);
            $this->layout = 'login';
            
	}
	
}